<template>
  <div class="app-container">
    
<br>
<br>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    > 
      <el-table-column align="center" label="序号" width="50">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="审批流名称" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.businessName }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="操作" align="center" width="300" >
        <template slot-scope="scope">
          <el-button type="primary"  @click="getInfo(scope.row)" size="small">详情</el-button>
          <el-button type="success"  @click="update(scope.row)" size="small">修改</el-button>
          <el-button type="danger" @click="deleteInfo(scope)" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="->, total, prev, pager, next"
      :total="this.total"
      :current-page="currentPage"
      :page-size="pageSize"
      @current-change="clickCurPage"
      >
    </el-pagination>
  <el-dialog title="审批流详情" :visible.sync="infoVisible" width="70%">
    <h2>审批流信息</h2>
    <br>
    <el-form ref="info" :model="info" label-width="120px">
      <el-form-item label="审批流名称">
          <el-input v-model="info.businessName" :disabled="true" style="width:90%;"/>
      </el-form-item>
      <br>
      <br>
      <el-form-item >
        <el-steps style="width:90%;">
          <el-step v-for="(item, index) in form.jobDutyList" :key="index" :title="item.name?item.deptName + ' - ' + item.name:''" description=""></el-step>
        </el-steps>
      </el-form-item>
      <br>
      <br>
      <el-form-item v-for="(jobDuty, index) in form.jobDutyList" :key="index" :label='(index+1)+"级审批"'>
        <el-select style="width:90%; margin-right: 10px;" v-model="jobDuty.id" placeholder="请选择" :disabled="true">
          <el-option
            v-for="item in jobDutyEnum"
            :key="item.id"
            :label="item.deptName + ' - ' + item.name"
            :value="item.id"
            @click.native="selectJobDuty(item, index)"
            >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-dialog>
  <el-dialog title="审批流修改" :visible.sync="formVisible" :closeOnClickModal="false" width="70%">
      <h2>审批流信息</h2>
      <br>
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="审批流名称">
            <el-input v-model="form.businessName" :disabled="true" style="width:90%;"/>
        </el-form-item>
        <br>
        <br>
        <el-form-item >
          <el-steps style="width:90%;">
            <el-step v-for="(item, index) in form.jobDutyList" :key="index" :title="item.name?item.deptName + ' - ' + item.name:''" description=""></el-step>
          </el-steps>
        </el-form-item>
        <br>
        <br>
        <el-form-item v-for="(jobDuty, index) in form.jobDutyList" :key="index" :label='(index+1)+"级审批"'>
          <el-select style="width:90%; margin-right: 10px;" v-model="jobDuty.id" placeholder="请选择" >
            <el-option
              v-for="item in jobDutyEnum"
              :key="item.id"
              :label="item.deptName + ' - ' + item.name"
              :value="item.id"
              @click.native="selectJobDuty(item, index)"
              >
            </el-option>
          </el-select>
          <el-button type="danger" icon="el-icon-delete" circle @click="deleteAuditDuty(index)"></el-button>
        </el-form-item>
        <el-button type="primary" @click="addaudit(form.jobDutyList)">添加审批级</el-button>
      </el-form>
      
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="confirm">确 定</el-button>
          <div style="width:200px;display: inline-block;"> </div>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
  </el-dialog>
  </div>
</template>

<script>

import { getList, edit, add, del} from '@/api/auditFlow'
import { getList as getJobDutyList, parentJobDuty} from '@/api/jobDuty'

export default {
  name: "AuditFlow",
  filters: {

  },
  data() {
    return {
      list: null,
      listLoading: true,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      curIndex: -1,
      infoVisible: false,
      formVisible: false,
      info: {},
      form: {},
      jobDutyEnum: [],
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList({currentPage: this.currentPage, pageSize: this.pageSize}).then(res=>{
        console.log(res)
        this.list = res.data
        this.total = res.data.length
        this.listLoading = false
        // enum
        getJobDutyList().then(deptRes=>{
          console.log("deptRes", deptRes)
          deptRes.data.unshift({id: 0, name: '默认上级', deptName: ''})
          this.jobDutyEnum = deptRes.data
        })
      }).catch(err=>{
        console.log("err")
        console.log(err)
        this.listLoading = false
      })
    },
    clickCurPage(val){
      this.currentPage = val
      this.fetchData()
    },
    getInfo(row) {
      this.info = row
      this.infoVisible = true
    },
    update(row) {
      this.reset()
      this.form = JSON.parse(JSON.stringify(row))
      this.formVisible = true
    },
    deleteInfo(scope) {
      console.log(scope.row)
      this.$confirm('确定删除该审批流?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(scope.row.id).then(res=>{
          this.list.splice(scope.$index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(err=>{
          console.log(err)
        })
        
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    confirm() {
      if(new Set(this.form.jobDutyList.map(o=>o.id)).size !== this.form.jobDutyList.length) {
        this.$message.error('重复设置职务')
        throw new Error("重复设置职务")
      }
      this.form.jobDutyList = this.form.jobDutyList.map(jobDuty => jobDuty.id).join(',')
      console.log(this.form)
      edit(this.form).then(res=>{
        console.log(res)
        this.$message({
          message: '修改成功',
          type: 'success'
        })
        this.formVisible = false
        this.listLoading = false
        this.$tab.refreshPage({path: '/user/auditFlow', name: "AuditFlow"});
      }).catch(err=>{
        console.log(err)
      })
      
      this.formVisible = false
    },
    cancel() {
      this.formVisible = false
    },
    reset(){
      this.form = {}
    },
    addaudit(jobDutys) {
      jobDutys.push({id: undefined, name: undefined})
    },
    selectJobDuty(item, index) {
      var curItem = JSON.parse(JSON.stringify(item))
      if(curItem.id === 0) {
        var jobDutyId = this.$store.getters.userInfo.jobDuty.split(',')[0]
        if(index != 0) jobDutyId = this.form.jobDutyList[index-1].id
        parentJobDuty({jobDutyId: jobDutyId}).then(res=> {
          this.form.jobDutyList[index].id = res.data.id
          this.form.jobDutyList[index].name = res.data.name
          this.form.jobDutyList[index].deptName = res.data.deptName
        }).catch(err=> {
          this.form.jobDutyList = this.form.jobDutyList.splice(0, index)
          return;
        })
      } else {
        this.form.jobDutyList[index].id = curItem.id
        this.form.jobDutyList[index].name = curItem.name
        this.form.jobDutyList[index].deptName = curItem.deptName
      }
      if(new Set(this.form.jobDutyList.map(o=>o.id)).size !== this.form.jobDutyList.length) {
        this.$message.error('重复设置职务')
        throw Error("重复设置职务")
      }
      

    },
    deleteAuditDuty(index) {
      this.form.jobDutyList.splice(index, 1)
    },

  }
}
</script>
